package com.onenine.app.listener;

import com.onenine.app.event.ConnectStatusEvent;
import com.onenine.chat.rongcloud.RcConnector;
import com.onenine.common.util.Logger;

import org.greenrobot.eventbus.EventBus;

import io.rong.imlib.RongIMClient;

/**
 * 创建时间：2018/6/1 0001 13:19
 * 创建人：fly
 * 功能描述：融云连接状态监听器
 */
public class OnConnectStatusListener implements RongIMClient.ConnectionStatusListener {

    public static final String RC_NETWORK_UNAVAILABLE = "网络连接不可用";
    public static final String RC_USER_BE_BLOCK = "用户被开发者后台封禁";
    public static final String RC_CONNECTING = "正在连接通信服务器";
    public static final String RC_DISCONNECT = "与通信服务器断开连接";
    public static final String RC_CONNECTED = "已连接";
    public static final String RC_KICKED_OFFLINE_BY_OTHER_CLIENT = "用户账号在其他设备登录";
    public static final String RC_SERVER_INVALID = "通信服务器异常或无法连接";
    public static final String RC_TOKEN_INCORRECT = "Token令牌不正确";

    @Override
    public void onChanged(ConnectionStatus connectionStatus) {
        Logger.e("融云连接状态：" + connectionStatus.getMessage());
        ConnectStatusEvent event = new ConnectStatusEvent();
        switch (connectionStatus) {
            case NETWORK_UNAVAILABLE:
                event.setShow(true);
                event.setMsg(RC_NETWORK_UNAVAILABLE);
                break;
            case CONNECTED:
                event.setShow(false);
                event.setMsg(RC_CONNECTED);
                break;
            case CONNECTING:
                event.setShow(true);
                event.setMsg(RC_CONNECTING);
                break;
            case DISCONNECTED:
                event.setShow(true);
                event.setMsg(RC_DISCONNECT);
                RcConnector.disconnect();
                break;
            case KICKED_OFFLINE_BY_OTHER_CLIENT:
                event.setShow(true);
                event.setMsg(RC_KICKED_OFFLINE_BY_OTHER_CLIENT);
                break;
            case TOKEN_INCORRECT:
                event.setShow(true);
                event.setMsg(RC_TOKEN_INCORRECT);
                break;
            case SERVER_INVALID:
                event.setShow(true);
                event.setMsg(RC_SERVER_INVALID);
                break;
            case CONN_USER_BLOCKED:
                event.setShow(true);
                event.setMsg(RC_USER_BE_BLOCK);
                break;
            default:
                return;
        }
        EventBus.getDefault().post(event);
    }

}
